﻿const express = require('express');
const bodyParser = require('body-parser');
const cookie = require('cookie-parser'); //cookies
const session = require('express-session'); // session
const multer = require('multer'); //上传文件所需的依赖包
const user = require('./handles/log_reg_Server'); //用户操作
const notice = require('./handles/noticeServer'); //公告相关操作
const dynamic = require('./handles/dynamicServer'); //协会动态相关操作
const course = require('./handles/courseService'); //轮滑教程
const video = require('./handles/videos_server'); //轮滑视频
const app = express();
app.listen(8083, function () {
    console.log('HTTPS Server is running on: http://localhost:%s', 8083);
})
//创建说明应该在哪里以及如何保存文件/图像的storage。
const Storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, "C://nginx/html/images"); //上传图片保存的路径
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + "_" + Date.now() + file.originalname); //+ "_" + file.originalname
    }
});
const upload = multer({
    storage: Storage
}).array("images", 1); //Field name and max count
// 这里multer接受我们在上一步中创建的storage作为参数。函数
app.use(bodyParser.json());
// app.use(cookie());
app.use(session({
    secret: 'login',
    resave: false,
    saveUninitialized: true,
    cookie: {
        httpOnly: true,
        maxAge: 60 * 120 * 1000,
    }
}))
// 创建 application/x-www-form-urlencoded 编码解析
const urlencodedParser = bodyParser.urlencoded({
    extended: false
});

app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://localhost:8080");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header('Access-Control-Allow-Credentials', 'true');
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
app.get('/getAllUsers', (req, res) => {
    //获取全部用户
    let users = new user();
    users.queryUsers(req, res);
})
app.post('/users/frezee', urlencodedParser, (req, res) => {
    //冻结用户
    let users = new user();
    users.frezee(req, res);
})
app.post('/users/batch', urlencodedParser, (req, res) => {
    //批量冻结用户
    let users = new user();
    users.batchFrezee(req, res);
})
app.get('/users/search', (req, res) => {
    let search = new user();
    search.search(req, res);
})
app.post('/user/login', urlencodedParser, (req, res) => {
    //登录接口
    let login = new user();
    login.login(req, res);
});
app.get('/user/getInfo', urlencodedParser, (req, res) => {
    //获取用户信息接口
    let login = new user();
    login.getUser(req, res);
});
app.get('/logout', urlencodedParser, (req,res) => {
    // 清楚用户登录信息
    let result = {};
    req.session.destroy();
    result.code = 1;
    res.write(JSON.stringify(result));
    res.end();
})
app.post('/user/register', urlencodedParser, (req, res) => {
    //注册接口
    let register = new user();
    register.register(req, res);
});
app.post('/upload', urlencodedParser, (req, res) => {
    //上传图片，并返回路径，如需预览，需加上https://www.xiandandan.xyz/folders/img/
    const result = {};
    upload(req, res, function (err) {
        let files = req.files;
        console.log(files[0].path);
        if (err) {
            result.code = 0;
            result.msg = '上传失败！'
        } else {
            result.code = 1;
            let path = files[0].path;
            let newPath = path.substring(path.indexOf('images') + 7, path.length);
            result.data = '/' + newPath;
        }
        res.write(JSON.stringify(result));
        res.end();
    });
})
app.post('/user/uploadHeadImage', urlencodedParser, (req, res) => {
    //上传头像
    let upload_ = new user();
    upload_.upload(req, res);
})
app.get('/noticeAll', (req, res) => {
    //查询所有公告
    let notice_ = new notice();
    notice_.queryAll(req, res);
})
app.post('/addNotice', urlencodedParser, (req, res) => {
    //新增公告
    let notice_ = new notice();
    notice_.add(req, res);
})
app.post('/delNotice', urlencodedParser, (req, res) => {
    //删除公告
    let notice_ = new notice();
    notice_.del(req, res);
})
app.post('/modifyNotice', urlencodedParser, (req, res) => {
    let modify = new notice();
    modify.modify(req, res);
})
app.get('/dynamic', (req, res) => {
    //查询所有协会动态
    let query = new dynamic();
    query.queryAll(req, res);
})
app.post('/operateDynamic', urlencodedParser, (req, res) => {
    let operate = new dynamic();
    if (req.body.type === 'add') {
        operate.addDynamic(req, res);
        return;
    } else if (req.body.type === 'del') {
        operate.delete(req, res);
        return;
    } else if (req.body.type === 'delAll') {
        operate.delAll(req, res);
        return;
    } else if (req.body.type === 'search') {
        operate.search(req, res);
    }
})
app.get('/course', (req, res) => {
    //查询教程
    let course_s = new course();
    course_s.queryCourse(req, res);
})
app.post('/course/operate', urlencodedParser, (req, res) => {
    //教程，增删改查
    let course_s = new course();
    if (req.body.method === 'add') {
        course_s.add(req, res);
        return;
    } else if (req.body.method === 'del') {
        course_s.del(req, res);
        return;
    } else if (req.body.method === 'delAll') {
        course_s.delAll(req, res);
        return;
    } else if (req.body.method === 'search') {
        course_s.search(req, res);
    }
})
app.get('/getVideos', (req, res) => {
    let videos = new video();
    videos.getAll(req, res);
})
app.post('/videos/operate', urlencodedParser, (req, res) => {
    let videos = new video();
    if (req.body.method === 'add') {
        videos.insertInto(req, res);
        return;
    } else if (req.body.method === 'del') {
        videos.del(req, res);
        return;
    } else if (req.body.method === 'delAll') {
        videos.delAll(req, res);
        return;
    } else if (req.body.method === 'search') {
        videos.search(req, res);
        return;
    }
});
// 发布心情时光轴
const timeline = require('./handles/timeline');
app.post('/timeline',urlencodedParser, (req,res) => {
    const time_line = new timeline();
    if(req.body.method === 'query') {
        time_line.query(req,res);
    } else {
        time_line.add(req,res);
    }
});
// 设置个人信息
app.post('/setInfo', urlencodedParser, (req,res) => {
    let users = new user();
    users.setinfo(req,res);
})